package redis.session ;

import java.util.Enumeration ;
import java.util.Map ;

import javax.servlet.ServletContext ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpSession ;
import javax.servlet.http.HttpSessionContext ;







import redis.RedisConfig ;
import springboot.app.common.SpringContextHolder ;
import utils.CookieUtil ;

/**
 * 使用redis来实现分布式session 注意每次session数据变更后都需要变更redis中的数据
 * @author 80002165 @date 2017年7月5日 上午9:06:56
 */
@SuppressWarnings("deprecation")
public class HttpSessionWrapper implements HttpSession {
    private String sid = "" ;
    private HttpSession session ;
    private HttpServletRequest request ;
    private HttpServletResponse response ;
    private Map<String, Object> map = null ;
    
    // 此处不能用自动注入，因为spring无法自动创建对象HttpSessionWrapper，没有提供无参构造函数
    SessionService sessionService = SpringContextHolder.getBean(SessionService.class) ;
    
    public HttpSessionWrapper(HttpSession session) {
        this.session = session ;
    }
    
    public HttpSessionWrapper(String sid, HttpSession session) {
        this(session) ;
        this.sid = sid ;
    }
    
    public HttpSessionWrapper(String sid, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        this(sid, session) ;
        this.request = request ;
        this.response = response ;
    }
    
    public Map<String, Object> getSessionMap() {
        if (map == null) {
            map = sessionService.getSession(sid) ;
        }
        return map ;
    }
    
    @Override
    public Object getAttribute(String key) {
        return getSessionMap().get(key) ;
    }
    
    @Override
    public void setAttribute(String key, Object value) {
        getSessionMap().put(key, value) ;
        sessionService.saveSession(sid, map) ;
    }
    
    @Override
    public void removeAttribute(String key) {
        getSessionMap().remove(key) ;
        sessionService.saveSession(sid, map) ;
    }
    
    @Override
    public Enumeration<String> getAttributeNames() {
        return null ;
    }
    
    /**
     * 初始化session
     * @author 80002165 @date 2017年7月5日 上午9:51:31
     */
    @Override
    public void invalidate() {
        getSessionMap().clear() ;
        sessionService.saveSession(sid, getSessionMap()) ;
        // 去掉cookie中session
        CookieUtil.removeCookieValue(this.request, this.response, RedisConfig.JSESSIONID) ;
    }
    
    @Override
    public String getId() {
        return sid ;
    }
    
    @Override
    public Object getValue(String arg0) {
        return this.session.getValue(arg0) ;
    }
    
    @Override
    public String[] getValueNames() {
        return this.session.getValueNames() ;
    }
    
    @Override
    public boolean isNew() {
        return this.session.isNew() ;
    }
    
    @Override
    public void putValue(String arg0, Object arg1) {
        this.session.putValue(arg0, arg1) ;
    }
    
    @Override
    public void removeValue(String arg0) {
        this.session.removeAttribute(arg0) ;
    }
    
    @Override
    public long getCreationTime() {
        return this.session.getCreationTime() ;
    }
    
    @Override
    public long getLastAccessedTime() {
        return this.session.getLastAccessedTime() ;
    }
    
    @Override
    public void setMaxInactiveInterval(int arg0) {
        this.session.setMaxInactiveInterval(arg0) ;
    }
    
    @Override
    public int getMaxInactiveInterval() {
        return this.session.getMaxInactiveInterval() ;
    }
    
    @Override
    public ServletContext getServletContext() {
        return this.session.getServletContext() ;
    }
    
    @Override
    public HttpSessionContext getSessionContext() {
        return this.session.getSessionContext() ;
    }
    
}
